layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 双证审核管理
     */
    var BtyDoubleCard = {
        tableId: "btyDoubleCardTable"
    };

    /**
     * 初始化表格的列
     */
    BtyDoubleCard.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'gcdm', sort: true, title: '工程店名'},
            {field: 'yaoma', sort: true, title: '妖码'},
            {field: 'yyjl', sort: true, title: '营运经理'},
            {field: 'dd', sort: true, title: '督导'},
            {field: 'frxm', sort: true, title: '法人姓名'},
            {field: 'frsfz', sort: true, title: '法人身份证'},
            {field: 'foodBusinessLicensePic', sort: true, title: '食品经营许可证图片'},
            {field: 'foodBusinessLicenseDate', sort: true, title: '食品经营许可证截止日期'},
            {field: 'businessLicensePic', sort: true, title: '营业执照图片'},
            {field: 'businessLicense', sort: true, title: '营业执照名称'},
            {field: 'businessLicenseAddress', sort: true, title: '营业执照地址'},
            {field: 'socialCreditCode', sort: true, title: '统一社会信用代码'},
            {field: 'createUser', sort: true, title: '添加人'},
            {field: 'createTime', sort: true, title: '添加时间'},
            {field: 'updateUser', sort: true, title: '操作人'},
            {field: 'updateTime', sort: true, title: '更新时间'},
            {field: 'status', sort: true, title: '状态 1未提交 2待审核 3已通过 4已驳回'},
            {field: 'rejectReason', sort: true, title: '驳回原因'},
            {field: 'proId', sort: true, title: '工程id'},
            {field: 'yyjlTel', sort: true, title: '营运经理电话'},
            {field: 'ddTel', sort: true, title: '督导电话'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyDoubleCard.search = function () {
        var queryData = {};

        queryData['gcdm'] = $('#gcdm').val();
        queryData['yyjl'] = $('#yyjl').val();
        queryData['dd'] = $('#dd').val();
        queryData['status'] = $('#status').val();

        table.reload(BtyDoubleCard.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyDoubleCard.openAddDlg = function () {
        func.open({
            title: '添加双证审核',
            content: Feng.ctxPath + '/btyDoubleCard/add',
            tableId: BtyDoubleCard.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyDoubleCard.openEditDlg = function (data) {
          func.open({
              title: '修改双证审核',
              content: Feng.ctxPath + '/btyDoubleCard/edit?id=' + data.id,
              tableId: BtyDoubleCard.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyDoubleCard.exportExcel = function () {
        var checkRows = table.checkStatus(BtyDoubleCard.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyDoubleCard.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyDoubleCard/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyDoubleCard.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyDoubleCard.tableId,
        url: Feng.ctxPath + '/btyDoubleCard/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyDoubleCard.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyDoubleCard.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyDoubleCard.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyDoubleCard.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyDoubleCard.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyDoubleCard.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyDoubleCard.onDeleteItem(data);
        }
    });
});
